#-*-coding=utf-8-*-

########### url ###################################
#(r'^login_or_reg', "core.auth.views.login_or_reg"),
#(r'^logout', "core.auth.views.logout"),
#(r'^not_admin_user', "core.auth.views.not_admin_user", {"template":"not_admin_user.html"}),
###################################################

from django.http import *
from core.auth.models import *
from datetime import datetime
from google.appengine.api import users
from django.shortcuts import render_to_response

def login_or_reg(request):
    request.user = users.get_current_user()
    profiles = Profile.gql("where user=:1", request.user)
    if profiles.count() == 0:
        Profile.create_profile(user = request.user)
    else:
        profile = profiles[0]
        profile.last_login = datetime.now()
        profile.put()
    return HttpResponseRedirect(request.GET["next"])


def logout(request):
    request.session.flush()
    if hasattr(request, 'user'):
        request.user = None
    return HttpResponseRedirect(request.GET["next"])


def not_admin_user(request, template):
    return render_to_response(template)
